<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/5/15
 * Time: 11:24
 */

namespace app\admin\logic;

use app\admin\logic\code\ErrorCode;
use app\common\model\Coupon;
use app\vehicle\model\CouponUser;
use think\Error;
use think\facade\Cache;

class CouponLogic extends BaseLogic{
    public function couponList($tempWhere,$nowPage){
        if ($tempWhere['title']){
            $where[] = ['title', 'like', "%".$tempWhere['title']."%"];
        }

        $count = Coupon::where($where)->count();
        if ($count > 0){
            $list = Coupon::where($where)->page($nowPage, self::$pageNum)->order('id desc')->select();
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $list, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }
    public function grantList($tempWhere,$nowPage){
        if ($tempWhere['name']){
            $where[] = ['u.nick_name', 'like', "%".$tempWhere['name']."%"];
        }
        if (is_numeric($tempWhere['source'])){
            $where[] = ['cu.source', '=', $tempWhere['source']];
        }
        $role = session('admin_info');
        if($role['role_id']==3) {
            $where[] = ['cu.user_id', 'eq',$role['shop_id']];
        }
        $count = CouponUser::alias('as cu')
            ->join(config('database.prefix').'user u','u.id=cu.user_id')
            ->where($where)->count();
        if ($count > 0){
            $list = CouponUser::alias('as cu')
            ->join(config('database.prefix').'user u','u.id=cu.user_id')
            ->where($where)->page($nowPage, self::$pageNum)
                ->field('cu.*,u.head_pic,u.nick_name')
                ->order('cu.id desc')->select();
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $list, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }
    public function info($id){
        return Coupon::get($id);
    }

    public function handle($data){
        $act = $data['act'];
        unset($data['act']);
        $res = false;
        switch ($act){
            case 'add':
                $insert['title'] = $data['title'];
                $insert['coupon_price'] = $data['coupon_price'];
                $insert['use_min_price'] = $data['use_min_price'];
                $insert['coupon_time'] = $data['coupon_time'];
                $insert['integral'] = $data['integral'];
                $insert['status'] = $data['status'];
                $insert['create_time'] = time();
                $res = Coupon::insert($insert);
                break;
            case 'edit':
                $update['title'] = $data['title'];
                $update['coupon_price'] = $data['coupon_price'];
                $update['use_min_price'] = $data['use_min_price'];
                $update['coupon_time'] = $data['coupon_time'];
                $update['integral'] = $data['integral'];
                $update['status'] = $data['status'];
                $update['update_time'] = time();
                $res = Coupon::where('id','eq',$data['id'])->update($update);
                break;
            case 'del':
                $article = Coupon::get($data['id']);
                $res = $article->delete();
                break;
            default:
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }
}